package de.gameduell.framework.gui.button {

	/**
	 * this class makes it possible to add several Togglebuttons to a Group that acts in predefined ways
	 */
	public class RadioButtonGroup {
		
		private var _buttons:Array;
		private var _inActiveAfterPressed:Boolean;
		
		
		/**
	 	* constructor
	 	*/
		public function RadioButtonGroup() {
			_buttons 			 = new Array();
			inActiveAfterPressed = true;
		}
		
		/**
	 	* @param newInActiveAfterPressed:Boolean  defines if the Button is deactivated after it was pressed. if the value of the 
	 	* variable is true, all other buttons in the group are automatically activated and untoggled while the used button is deactivated
	 	*/
		public function set inActiveAfterPressed(newInActiveAfterPressed:Boolean):void{
			_inActiveAfterPressed = newInActiveAfterPressed;
		}
		
		public function get buttons():Array{
			return _buttons;
		}
		
		public function get inActiveAfterPressed():Boolean{
			return _inActiveAfterPressed;
		}
		
		
		/**
	 	* returns the button that is currently toggled
	 	*/
		public function getToggledButton():ToggleButton{
			var returnValue:ToggleButton;
			for(var i:int=0; i < _buttons.length; i++){
				if(ToggleButton(_buttons[i]).toggled == true){
					returnValue = ToggleButton(_buttons[i]);
				}
			}
			return returnValue;
		}
		
		
		/**
	 	* adds a button to the group
	 	*/
		public function addButton(button:ToggleButton):void{
			_buttons.push(button);
			button.addEventListener(Button.CLICK, catchButtonEvent);
		}
		
		private function catchButtonEvent(event:ButtonEvent):void{
			var button:ToggleButton = ToggleButton(event.button);
			buttonPressed(button);
		}
	
		public function setButtonActive(index:int):void{
			buttonPressed(ToggleButton(buttons[index]));
		}
		
		/**
	 	* removes a button from the group
	 	*/
		public function removeButton(button:ToggleButton):void{
			for(var i:int = 0; i < _buttons.length; i++){
				if(button == _buttons[i]){
					_buttons.splice(i,1);
				}
			}
		}
		
		public function set active(isActive:Boolean):void{
			for(var i:int = 0; i < _buttons.length; i++){
				ToggleButton(_buttons[i]).active = isActive;
			}
		}
		
		/**
	 	* all buttons are untoggled
	 	*/
		public function reset():void{
			for(var i:int = 0; i < _buttons.length; i++){
				ToggleButton(_buttons[i]).toggled = false;
				ToggleButton(_buttons[i]).active = true;
			}
		}
		
		/**
	 	* @param index:int  the index of the button that will be toggled
	 	* all other buttons in the group will be untoggled
	 	*/
		public function toggleButton(index:int):void{
			for(var i:int = 0; i < _buttons.length; i++){
				if(i != index){
					ToggleButton(_buttons[i]).toggled = false;
				}else{
					ToggleButton(_buttons[i]).toggled = true;
					if(_inActiveAfterPressed){
						ToggleButton(_buttons[i]).active = false;
					}
				}
			}
		}
				
		/**
	 	* @param button the button that was pressed	 	* 
	 	* if a button was pressed all other buttons in the group are untoggled and according to the _inActiveAfterPressed
	 	* variable activated or not. the same goes for the activation state of the pressed button.
	 	*/
		private function buttonPressed(button:ToggleButton):void{
			for(var i:int = 0; i < _buttons.length; i++){
				if(_buttons[i] != button){
					ToggleButton(_buttons[i]).active  = _inActiveAfterPressed ? (ToggleButton(_buttons[i]).toggled ? true:_buttons[i].active) : _buttons[i].active;
					ToggleButton(_buttons[i]).toggled = false;
				}else{
					button.active  = _inActiveAfterPressed ? false : button.active ;
				}
			}
		}
	}
}
